from openpyxl import load_workbook
import sys


class TasksManager:
    def __init__(self, accounts_list_filename):
        self.tasks = []
        self.gh_set = set()
        try:
            self.workbook = load_workbook(accounts_list_filename)
            self.sheet = self.workbook.active
            self.columns = ['gh_id']
            for row_index in range(2, self.sheet.max_row + 1):
                task = {}
                for index, column in enumerate(self.columns):
                    task[column] = self.sheet.cell(row=row_index, column=index + 1).value
                if not task['gh_id']:
                    continue
                task['gh_id'] = task['gh_id'].strip()
                if task['gh_id'] in self.gh_set:
                    print('任务列表中含有重复ID：{}，请检查'.format(task['gh_id']))
                    sys.exit(1)
                self.tasks.append(task)
                self.gh_set.add(task['gh_id'])
        except FileNotFoundError:
            print('任务列表不存在，请建立文件“{}”'.format(accounts_list_filename))

    def get_tasks(self):
        # prevent modify
        return self.tasks[:]

    def get_task_chunks(self, size):
        tasks = self.get_tasks()
        return [tasks[i * size:(i + 1) * size] for i in range((len(tasks) + size - 1) // size)]

    def describe_tasks(self):
        for task in self.get_tasks():
            print(task)


def default_manager():
    return TasksManager('账号素材修改.xlsx')


if __name__ == '__main__':
    m = TasksManager(sys.argv[1]) if len(sys.argv) >= 2 else default_manager()
    m.describe_tasks()
